﻿"use strict";
define(['admin/app', "service/admin/productTokenMakerService", "angular-kindeditor", "ui.file.manage"], function (app) {
    app.controller('productTokenMakerController', function ($scope, $stateParams, $location, $uibModal, $state, utils, productTokenMakerService) {
        $scope.query = {
            page: 1, size: 20, q: '', product_name: ''
        };
        $scope.product_query = {
            page: 1, size: 20, keyword: ''
        }
        $.extend($scope.query, $stateParams);
        //商品弹窗选择
        var data = "";
        var index = "";
        $scope.addGood = function () {
            index = $stateParams.id;
            var modalInstance = $uibModal.open({
                templateUrl: 'modal.html',//script标签中定义的id
                controller: 'modalCtrl',//modal对应的Controller
                windowClass: 'large-Modal',
                resolve: {
                    data: function () {//data作为modal的controller传入的参数
                        return data = productTokenMakerService.getproducts($scope.product_query).success(function (response) {
                            return response.data;//用于传递数据
                        });
                    },
                    index: function () {
                        //对应分组索引
                        return index;
                    },
                    parentScope: function () {
                        return $scope;
                    }
                }
            })
        };
        //模态框对应的Controller
        app.controller('modalCtrl', function ($scope, $uibModalInstance, parentScope, data, index) {
            $scope.data = data.data.data;
            //在这里处理要进行的操作
            $scope.ok = function () {
                var id = $("input[type='radio']:checked");
                if (id.length == 0) {
                    utils.notify("请选择商品", "warning");
                    $uibModalInstance.close();
                    return;
                }
                productTokenMakerService.addProductTokenMaker(id.val()).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    parentScope.search();
                });
                $uibModalInstance.close();
            };

            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };

            $scope.search_page = function (page) {
                parentScope.product_query.page = page;
                productTokenMakerService.getproducts(parentScope.product_query).success(function (response) {
                    $scope.data = response.data;//用于传递数据
                });
            };

            $scope.getProducts = function () {
                parentScope.product_query.keyword = $scope.keyword;
                productTokenMakerService.getproducts(parentScope.product_query).success(function (response) {
                    $scope.data = response.data;//用于传递数据
                });
            };

            $scope.selectTr = function (id) {
                /*当单击表格行时,把单选按钮设为选中状态*/
                $("#product_tr_" + id).find("input[type='radio']").prop("checked", true);
            };
        });
        var methods = {
            search: function () {
                productTokenMakerService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    productTokenMakerService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '编辑';
                } else {
                    productTokenMakerService.get({ id: 0 }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '新增';
                }
            },
            save: function () {
                var modal = utils.confirm({ msg: "确定要保存该创客商品么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    var model = $scope.model;
                    $scope.psdisabled = true;
                    productTokenMakerService.update(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                });
            },
            remove: function (id) {
                var modal = utils.confirm({ msg: "是否需要删除该创客商品（注意：将该商品的所有规格都不再参与创客商品）", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    productTokenMakerService.delete(id).success(function () {
                        methods.search();
                    });
                })
            },
            goBack: function () {
                history.back();
            },
            search_page: function (page) {
                $scope.query.page = page;
                methods.search('page', $scope.query.page);
            }
        }

        angular.extend($scope, methods);
        if ($state.includes('app.productTokenMaker.detail')) {
            methods.edit();
        }else {
            methods.search();
        }
    });
});
